<template>
	<view>
		<image src="../../static/logo-2.png" mode="widthFix" class="register"></image>
		<view class="register-container">
			<input type="text" placeholder="输入你的邀请码" class="register-code" maxlength="6" v-model="registerCode">
			<view class="register-desc">
				管理员创建员工账号后，你可以从你的个人邮箱中获得注册邀请码
			</view>
			<button class="register-btn" @tap="register()">执行注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				registerCode: '',
				code: null
			}
		},
		methods: {
			register: function() {
				let that = this;
				if (that.registerCode == null || that.registerCode.length == 0) {
					uni.showToast({
						title: '邀请码不能为空',
						icon: 'none'
					});
					return
				} else if (/^[0-9]{6}$/.test(that.registerCode) == false) {
					uni.showToast({
						icon: "none",
						title: '邀请码必须是6位数字'
					});
					return
				}
				uni.login({
					provider: 'weixin',
					success: function(resp) {
						//获取临时授权字符串
						let code = resp.code;
						that.code = code;
						// console.log("临时授权码:" + code);
					}
				});
				// 获取用户信息
				uni.getUserProfile({
					desc: '获取用户信息',
					success: function(resp) {
						let nickName = resp.userInfo.nickName;
						let avatarUrl = resp.userInfo.avatarUrl;
						//console.log("nickname:" + resp.userInfo.nickName);
						let data = {
							code: that.code,
							nickname: nickName,
							photo: avatarUrl,
							registerCode: that.registerCode
						};
						//console.log("url:" + that.url.register);
						//console.log('data:' + data);
						that.ajax(that.url.register, 'POST', data, function(resp) {
							let permission = resp.data.permission;
							let token = resp.data.token;
							uni.setStorageSync('permission', permission)
							uni.setStorageSync('token', token)
							//console.log("per:" + permission);
							// 跳转登录页面
							uni.switchTab({
								url: '../index/index'
							})
						})
					},
					fail() {
						console.log('执行出错');
					}
				});
			}
		}
	}
</script>

<style lang="less">
	@import url("register.less");
</style>
